﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Security;
using AzureHelpers;
using BackupObjects.Taging;
using Microsoft.Samples.ServiceHosting.StorageClient;
using NUnit.Framework;


namespace BackupObjects.Test
{
    [TestFixture]
    public class FilesTest
    {
        const string Username = BackupRepository.UserNameAdmin;
        [SetUp]
        public void FilesSetUp()
        {
            FirstRequestInitialization.Initialize(BackupRepository.UserNameAdmin);
            //ensure not backups for admin
            BackupRepository brDelete = new BackupRepository();
            brDelete.LoadAll(Username);
            foreach (var backup in brDelete.Backups)
            {
                brDelete.Delete(backup.ID, Username);
            }
        }
        [Test]
        public void TestAddAndRetrieveFile()
        {
            
            string filename = "backup.txt";
           
            
            

            BackupObject bo = new BackupObject();
            bo.OriginalFileName = "backup.txt";
            bo.Name = "backup.txt";            
            using (StreamReader backupcontent = new StreamReader("backup.txt"))
            {
                using (BinaryReader read = new BinaryReader(backupcontent.BaseStream))
                {
                    bo.Contents = read.ReadBytes((int)backupcontent.BaseStream.Length);
                }
            }
            bo.UploadBy = Membership.GetUser(Username).ProviderUserKey.ToString();
            

            BackupRepository br = new BackupRepository();
            br.Save(bo);

            BackupRepository brNew = new BackupRepository();
            brNew.LoadAll(Username);
            Assert.That(brNew.Backups.Count, Is.EqualTo(1));
            var src = br.GetBackupStreamIco(brNew.Backups[0].ID,Username);
            Assert.That(src, Is.Not.Null, "must have an .ico attached");

            brNew.Delete(brNew.Backups[0], Username);

            var brDelete = new BackupRepository();
            brDelete.LoadAll(Username);
            Assert.That(brDelete.Backups.Count, Is.EqualTo(0));

        }
    }
}
